home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / MacCleo / geoface / make_face.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  14.0 KB  |  536 lines

  1. /* ==========================================================================
  2.                           MAKE_FACE_C
  3. =============================================================================
  4.  
  5.     FUNCTION NAMES
  6.  
  7.     face_reset            -- reset the face to neutral.
  8.     create_face                 -- creates the face data
  9.     create_face            -- returns a pointer to the head datastructure.
  10.     make_face            -- makes a face from the two input files.
  11.     add_polygon_to_face        -- adds a polygon to the face data structure.
  12.     reflect_polygon         -- reflect a polygon in the Y axis.
  13.     void averaged_vertex_normals - compute the average vertex normals.
  14.     data_struct         -- create the datastructure for the face.
  15.  
  16.     C SPECIFICATIONS
  17.  
  18.     void face_reset     ( HEAD *face )
  19.     HEAD *create_face     ( char *f1, char *f2 )    
  20.     HEAD *create_face     ( f1, f2 )
  21.     make_face         ( HEAD *face ) 
  22.     add_polygon_to_face ( POLYGON *p, HEAD *face )
  23.     reflect_polygon     ( POLYGON *poly, HEAD *face ) 
  24.     void averaged_vertex_normals ( HEAD *face, int p, 
  25.                                    float *n1, float *n2, float *n3 ) 
  26.     data_struct     ( HEAD *face )
  27.  
  28.     DESCRIPTION
  29.  
  30.     This module is where the face data structures are created.
  31.     T his module comes as is with no warranties.  
  32.  
  33.     HISTORY
  34.     16-Dec-94  Keith Waters (waters) at DEC's Cambridge Research Lab
  35.     Created.
  36.     Modified 22-Nov-96 Sing Bing Kang (sbk@crl.dec.com)
  37.       Added function expressions() to enable changing facial expression
  38.  
  39. ============================================================================ */
  40.  
  41. #include <math.h>           /* C header for any math functions               */
  42. #include <stdio.h>          /* C header for standard I/O                     */
  43. #include <string.h>         /* For String compare                            */
  44. #include <stdlib.h>
  45. #ifndef _WIN32
  46. #include <sys/types.h>
  47. #include <sys/file.h>
  48. #endif
  49.  
  50. #include "memory.h"         /* Local memory allocation macros                */
  51. #include "head.h"        /* local header for the face             */
  52.  
  53. void reflect_polygon ( POLYGON *poly, HEAD *face );
  54. void add_polygon_to_face ( POLYGON *p, HEAD *face );
  55. void make_face ( HEAD *face );
  56.  
  57. /* ========================================================================= */  
  58. /* face_reset                                                                 */
  59. /* ========================================================================= */  
  60. /*
  61. ** Resets the geometry of the face to neutral.
  62. **
  63. */
  64.  
  65. void face_reset ( HEAD *face )
  66. {
  67.   int i,j,k ;
  68.  
  69.   
  70.   for ( i=0; i<face->npolygons; i++ ) {
  71.  
  72.     for ( j=0; j<3; j++ ) {
  73.  
  74.       for ( k=0; k<3; k++ ) {
  75.     face->polygon[i]->vertex[j]->xyz[k] = 
  76.       face->polygon[i]->vertex[j]->nxyz[k] ;
  77.       }
  78.     }
  79.   }
  80. }
  81.  
  82.  
  83. /* ========================================================================= 
  84.    expressions                                                            
  85.    Written by: Sing Bing Kang
  86.    Date: 11/22/96
  87.    ========================================================================= */  
  88. /*
  89. ** Produces the facial expressions as indicated by the muscle contraction
  90. ** vector
  91. **
  92. */
  93.  
  94. void
  95. expressions ( HEAD *face, int e )
  96. {
  97.     int m;
  98.  
  99.     fprintf( stderr, "Expression: %s\n", face->expression[e]->name );
  100.  
  101.     for (m=0; m<face->nmuscles; m++) {
  102.     float m_val = face->expression[e]->m[m],
  103.               m_diff = m_val - face->muscle[m]->mstat;
  104.         
  105.         if (m_diff) {
  106.             face->muscle[m]->mstat = m_val;
  107.             activate_muscle ( face,
  108.                             face->muscle[m]->head,
  109.                             face->muscle[m]->tail,
  110.                             face->muscle[m]->fs,
  111.                             face->muscle[m]->fe,
  112.                             face->muscle[m]->zone,
  113.                             m_diff ) ;
  114.         }
  115.     }
  116.  
  117. }
  118.  
  119. /* ========================================================================= */  
  120. /* create_face                                                                 */
  121. /* ========================================================================= */  
  122. /*
  123. ** create the default structures for the face and retrun a pointer.
  124. **
  125. */
  126.  
  127. HEAD *create_face ( char *f1, char *f2 )
  128. {
  129.   HEAD *h ;
  130.   
  131.   h = _new ( HEAD ) ;
  132.   
  133.   h->npolygons        = 0 ;
  134.   h->npindices        = 0 ;
  135.   h->npolylinenodes    = 0 ;
  136.   h->nmuscles        = 0 ;
  137.   
  138.   h->nexpressions    = 0;
  139.   h->expression        = NULL;
  140.   
  141.  
  142.   read_polygon_indices ( f1, h ) ;
  143.   read_polygon_line    ( f2, h ) ;
  144.  
  145.   make_face ( h ) ;
  146.   
  147.   return ( h ) ;
  148.  
  149. }
  150.  
  151. /* ========================================================================= */  
  152. /* make_face                                                                 */
  153. /* ========================================================================= */  
  154. /*
  155. ** makes the face from the two input files.
  156. **
  157. */
  158.  
  159. void
  160. make_face ( HEAD *face ) 
  161. {
  162.   POLYGON  *p ;
  163.   int i, ii, j, k,
  164.       p1, p2, p3, p4 ;
  165.   int parray[4] ;
  166.  
  167.   for ( i=0, ii=0; i < face->npindices; i++,ii+=4 ) {
  168.  
  169.     p1 = face->indexlist[ii]   -1 ;
  170.     p2 = face->indexlist[ii+1] -1 ;
  171.     p3 = face->indexlist[ii+2] -1 ;
  172.     p4 = face->indexlist[ii+3] -1 ;
  173.  
  174.     for (j=0; j<4; j++)
  175.       parray[j] = face->indexlist[ii+j] -1;
  176.  
  177.     if ( p1 == 999 ) {
  178.  
  179.       p = _new ( POLYGON ) ;
  180.       for (j=0; j<3; j++) {
  181.     p->vertex[j] = _new ( VERTEX ) ;
  182.     p->vertex[j]->np = 0 ;
  183.       }
  184.  
  185.       for (j=0; j<3; j++) 
  186.     p->vertex[0]->nxyz[j] = 
  187.     p->vertex[0]->xyz[j]  = face->polyline[ p2*3 + j ] ;
  188.       
  189.       for (j=0; j<3; j++) 
  190.     p->vertex[1]->nxyz[j] = 
  191.         p->vertex[1]->xyz[j]  = face->polyline[ p3*3 + j ] ;
  192.       
  193.       for (j=0; j<3; j++) 
  194.     p->vertex[2]->nxyz[j] = 
  195.         p->vertex[2]->xyz[j]  = face->polyline[ p4*3 + j ] ;
  196.  
  197.       add_polygon_to_face ( p, face ) ;
  198.       reflect_polygon     ( p, face ) ;
  199.     }
  200.     else {
  201.       p = _new ( POLYGON ) ;
  202.       for (j=0; j<3; j++) {
  203.     p->vertex[j] = _new ( VERTEX ) ;
  204.     p->vertex[j]->np = 0 ;
  205.       }
  206.  
  207.       for (k=0; k<3; k++) {
  208.     for (j=0; j<3; j++) 
  209.       p->vertex[k]->nxyz[j] =
  210.       p->vertex[k]->xyz[j]  = face->polyline[ parray[k]*3 + j ] ;
  211.       }
  212.  
  213.       add_polygon_to_face ( p, face ) ;
  214.       reflect_polygon     ( p, face ) ;
  215.  
  216.       p = _new ( POLYGON ) ;
  217.       for (j=0; j<3; j++) {
  218.     p->vertex[j] = _new ( VERTEX ) ;
  219.     p->vertex[j]->np = 0 ;
  220.       }
  221.  
  222.       for (j=0; j<3; j++) 
  223.     p->vertex[0]->nxyz[j] = 
  224.     p->vertex[0]->xyz[j]  = face->polyline[ p1*3 + j ] ;
  225.       
  226.       for (j=0; j<3; j++) 
  227.     p->vertex[1]->nxyz[j] =
  228.     p->vertex[1]->xyz[j]  = face->polyline[ p3*3 + j ] ;
  229.       
  230.       for (j=0; j<3; j++) 
  231.     p->vertex[2]->nxyz[j] = 
  232.     p->vertex[2]->xyz[j]  = face->polyline[ p4*3 + j ] ;
  233.  
  234.       add_polygon_to_face ( p, face ) ;
  235.       reflect_polygon     ( p, face ) ;
  236.       }
  237.   }
  238.  
  239. }
  240.  
  241. /* ========================================================================= */  
  242. /* add_polygon_to_face                                                       */
  243. /* ========================================================================= */  
  244. /*
  245. ** add a polygon to the face structure.
  246. **
  247. */
  248.  
  249. void
  250. add_polygon_to_face ( POLYGON *p, HEAD *face )
  251. {
  252.   int nn ;
  253.  
  254.   if(face->npolygons == 0)
  255.       face->polygon = _new_array(POLYGON *, 500) ;
  256.   else if(face->npolygons % 500 == 0)
  257.       face->polygon = _resize_array(face->polygon,POLYGON *,face->npolygons+500) ;
  258.  
  259.   nn = face->npolygons ;
  260.   face->polygon[nn] = p ;
  261.  
  262.   face->npolygons++ ;
  263.  
  264. }
  265.  
  266.  
  267. /* ========================================================================= */  
  268. /* reflect_polygon                                                           */
  269. /* ========================================================================= */  
  270. /*
  271. **  Reflects all the polygons in the half-face and adds them to
  272. **  the data structure.
  273. **
  274. */
  275.  
  276. void
  277. reflect_polygon ( POLYGON *poly, HEAD *face ) 
  278. {
  279.   POLYGON *newp ;
  280.   float   temp[3] ;
  281.   int      i, j ;
  282.   
  283.   /* 
  284.    * Allocate memory for the new polygon. 
  285.   */
  286.   newp = _new ( POLYGON ) ;
  287.   for (j=0; j<3; j++) {
  288.     newp->vertex[j] = _new ( VERTEX ) ;
  289.     newp->vertex[j]->np = 0 ;
  290.   }
  291.  
  292.   /* 
  293.    * Load the old polygon values. 
  294.   */
  295.   for (i=0; i<3; i++) 
  296.     for (j=0; j<3; j++)
  297.       newp->vertex[i]->nxyz[j] = 
  298.       newp->vertex[i]->xyz[j]  = poly->vertex[i]->xyz[j] ;
  299.  
  300.   /* 
  301.    * flip the X component.         
  302.   */
  303.   for (i=0; i<3; i++) 
  304.     newp->vertex[i]->nxyz[0] = 
  305.     newp->vertex[i]->xyz[0]  = -newp->vertex[i]->xyz[0] ;
  306.   
  307.   /* 
  308.    * Re-order the vertices, flip 0 and 1.
  309.   */
  310.   for (j=0; j<3; j++)
  311.     temp[j] = newp->vertex[0]->xyz[j] ;
  312.  
  313.   for (j=0; j<3; j++)
  314.     newp->vertex[0]->nxyz[j] = 
  315.     newp->vertex[0]->xyz[j]  = newp->vertex[1]->xyz[j];
  316.  
  317.   for (j=0; j<3; j++)
  318.     newp->vertex[1]->nxyz[j] = 
  319.     newp->vertex[1]->xyz[j]  = temp[j] ;
  320.  
  321.   add_polygon_to_face ( newp, face ) ;
  322.  
  323. }
  324.  
  325. /* ========================================================================= */  
  326. /* averaged_vertex_normals                                                   */
  327. /* ========================================================================= */  
  328. /*
  329. ** Caculates the averaged polygon normal.
  330. */
  331.  
  332. void averaged_vertex_normals ( HEAD *face, int p, float *n1, float *n2, float *n3 ) 
  333. {
  334.   int i,j,np, pt ;
  335.   float norm[3] ;
  336.  
  337.  
  338.   for (i=0; i<3; i++)
  339.     norm[i] = 0.0 ;
  340.  
  341.   np = face->polygon[p]->vertex[0]->np ;
  342.  
  343.   for ( i=0; i<np; i++) {
  344.     pt = face->polygon[p]->vertex[0]->plist[i] ;
  345.  
  346.     for ( j=0; j<3; j++)  {
  347.       norm[j] += face->polygon[pt]->vertex[0]->norm[j] ;
  348.     }
  349.   }
  350.  
  351.   for (i=0; i<3; i++)
  352.     norm[i] = norm[i] / (float)np ;
  353.     
  354.   for (i=0; i<3; i++)
  355.     n1[i] = norm[i] ;
  356.  
  357.   for (i=0; i<3; i++)
  358.     norm[i] = 0.0 ;
  359.  
  360.   np = face->polygon[p]->vertex[1]->np ;
  361.  
  362.   for ( i=0; i<np; i++) {
  363.     pt = face->polygon[p]->vertex[1]->plist[i] ;
  364.  
  365.     for ( j=0; j<3; j++) {
  366.       norm[j] += face->polygon[pt]->vertex[1]->norm[j] ;
  367.     }
  368.   }
  369.  
  370.   for (i=0; i<3; i++)
  371.     norm[i] = norm[i] / (float) np ;
  372.  
  373.   for (i=0; i<3; i++)
  374.     n2[i] = norm[i] ;
  375.  
  376.   for (i=0; i<3; i++)
  377.     norm[i] = 0.0 ;
  378.  
  379.   np = face->polygon[p]->vertex[2]->np ;
  380.  
  381.   for ( i=0; i<np; i++) {
  382.     pt = face->polygon[p]->vertex[2]->plist[i] ;
  383.  
  384.     for ( j=0; j<3; j++) {
  385.       norm[j] += face->polygon[pt]->vertex[2]->norm[j] ;
  386.     }
  387.   }
  388.  
  389.   for (i=0; i<3; i++)
  390.     norm[i] = norm[i]/ (float) np ;
  391.     
  392.   for (i=0; i<3; i++)
  393.     n3[i] = norm[i] ;
  394.    
  395. }
  396.  
  397. /* ========================================================================= */  
  398. /* data_struct                                                                 */
  399. /* ========================================================================= */  
  400. /*
  401. ** Create a new data structure for the polygons.
  402. **
  403. */
  404. #define DATA_STRUCT_DEBUG 0
  405.  
  406. void
  407. data_struct ( HEAD *face )
  408. {
  409.   int i,j, n ;
  410.   int flag, cptr ;
  411.   float x1,y1,z1, x2, y2, z2, x3, y3, z3 ;
  412.   float tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3 ;
  413.  
  414.   for (i=0; i<face->npolygons; i++ ){
  415.  
  416.       x1 = face->polygon[i]->vertex[0]->xyz[0] ;
  417.       y1 = face->polygon[i]->vertex[0]->xyz[1] ;
  418.       z1 = face->polygon[i]->vertex[0]->xyz[2] ;
  419.  
  420.       x2 = face->polygon[i]->vertex[1]->xyz[0] ;
  421.       y2 = face->polygon[i]->vertex[1]->xyz[1] ;
  422.       z2 = face->polygon[i]->vertex[1]->xyz[2] ;
  423.  
  424.       x3 = face->polygon[i]->vertex[2]->xyz[0] ;
  425.       y3 = face->polygon[i]->vertex[2]->xyz[1] ;
  426.       z3 = face->polygon[i]->vertex[2]->xyz[2] ;
  427. #if DATA_STRUCT_DEBUG
  428.       fprintf (stderr,"BASE polygon: %d\n", i) ;
  429.       fprintf (stderr,"x1: %f y1: %f z1: %f\n", x1,y1,z1) ;
  430.       fprintf (stderr,"x1: %f y1: %f z1: %f\n", x2,y2,z2) ;
  431.       fprintf (stderr,"x1: %f y1: %f z1: %f\n", x3,y3,z3) ;
  432. #endif
  433.       j    = 0 ;
  434.       flag = 0 ;
  435.       while ( !flag  &&
  436.           j<face->npolygons ) {
  437.  
  438.     tx1 = face->polygon[j]->vertex[0]->xyz[0] ;
  439.     ty1 = face->polygon[j]->vertex[0]->xyz[1] ;
  440.     tz1 = face->polygon[j]->vertex[0]->xyz[2] ;
  441.  
  442.     tx2 = face->polygon[j]->vertex[1]->xyz[0] ;
  443.     ty2 = face->polygon[j]->vertex[1]->xyz[1] ;
  444.     tz2 = face->polygon[j]->vertex[1]->xyz[2] ;
  445.  
  446.     tx3 = face->polygon[j]->vertex[2]->xyz[0] ;
  447.     ty3 = face->polygon[j]->vertex[2]->xyz[1] ;
  448.     tz3 = face->polygon[j]->vertex[2]->xyz[2] ;
  449. #if DATA_STRUCT_DEBUG
  450.     fprintf (stderr, "COMPARED TO polygon: %d\n", j) ;
  451.     fprintf (stderr,"tx1: %f ty1: %f tz1: %f\n", tx1,ty1,tz1) ;
  452.     fprintf (stderr,"tx1: %f ty1: %f tz1: %f\n", tx2,ty2,tz2) ;
  453.     fprintf (stderr,"tx1: %f ty1: %f tz1: %f\n", tx3,ty3,tz3) ;
  454. #endif    
  455.     if ( (x1 == tx1 && y1 == ty1 && z1 == tz1) ||
  456.          (x1 == tx2 && y1 == ty2 && z1 == tz2) ||
  457.          (x1 == tx3 && y1 == ty3 && z1 == tz3)) {
  458.       cptr = j ;
  459. #if DATA_STRUCT_DEBUG
  460.       fprintf (stderr,"found a vertex match on polygon: %d and %d\n", i,j);
  461. #endif
  462.       n = face->polygon[i]->vertex[0]->np ;
  463.       face->polygon[i]->vertex[0]->plist[n] = cptr ;
  464.       face->polygon[i]->vertex[0]->np++ ;
  465. #if DATA_STRUCT_DEBUG
  466.       fprintf (stderr,"loaded: %d onto polygon: %d vertex[0]\n", cptr, i) ;
  467.       fprintf (stderr,"total on vertex: %d\n", face->polygon[i]->vertex[0]->np);
  468. #endif
  469.     }
  470.     j++ ;
  471.  
  472.       } /* end while */
  473.  
  474.  
  475.       j    = 0 ;
  476.       flag = 0 ;
  477.       while ( !flag  &&
  478.           j<face->npolygons ) {
  479.     tx1 = face->polygon[j]->vertex[0]->xyz[0] ;
  480.     ty1 = face->polygon[j]->vertex[0]->xyz[1] ;
  481.     tz1 = face->polygon[j]->vertex[0]->xyz[2] ;
  482.  
  483.     tx2 = face->polygon[j]->vertex[1]->xyz[0] ;
  484.     ty2 = face->polygon[j]->vertex[1]->xyz[1] ;
  485.     tz2 = face->polygon[j]->vertex[1]->xyz[2] ;
  486.  
  487.     tx3 = face->polygon[j]->vertex[2]->xyz[0] ;
  488.     ty3 = face->polygon[j]->vertex[2]->xyz[1] ;
  489.     tz3 = face->polygon[j]->vertex[2]->xyz[2] ;
  490.  
  491.     if ( (x2 == tx1 && y2 == ty1 && z2 == tz1) ||
  492.          (x2 == tx2 && y2 == ty2 && z2 == tz2) ||
  493.          (x2 == tx3 && y2 == ty3 && z2 == tz3)) {
  494.       cptr = j ;
  495.  
  496.       n = face->polygon[i]->vertex[1]->np ;
  497.       face->polygon[i]->vertex[1]->plist[n] = j ;
  498.       face->polygon[i]->vertex[1]->np++ ;
  499.  
  500.     }
  501.     j++ ;
  502.  
  503.       } /* end while */
  504.  
  505.       j    = 0 ;
  506.       flag = 0 ;
  507.       while ( !flag  &&
  508.           j<face->npolygons ) {
  509.     tx1 = face->polygon[j]->vertex[0]->xyz[0] ;
  510.     ty1 = face->polygon[j]->vertex[0]->xyz[1] ;
  511.     tz1 = face->polygon[j]->vertex[0]->xyz[2] ;
  512.  
  513.     tx2 = face->polygon[j]->vertex[1]->xyz[0] ;
  514.     ty2 = face->polygon[j]->vertex[1]->xyz[1] ;
  515.     tz2 = face->polygon[j]->vertex[1]->xyz[2] ;
  516.  
  517.     tx3 = face->polygon[j]->vertex[2]->xyz[0] ;
  518.     ty3 = face->polygon[j]->vertex[2]->xyz[1] ;
  519.     tz3 = face->polygon[j]->vertex[2]->xyz[2] ;
  520.  
  521.     if ( x3 == tx1 &&  y3 == ty1 &&  z3 == tz1 ||
  522.          x3 == tx2 &&  y3 == ty2 &&  z3 == tz2 ||
  523.          x3 == tx3 &&  y3 == ty3 &&  z3 == tz3) {
  524.       cptr = j ;
  525.  
  526.       n = face->polygon[i]->vertex[2]->np ;
  527.       face->polygon[i]->vertex[2]->plist[n] = cptr ;
  528.       face->polygon[i]->vertex[2]->np++ ;
  529.  
  530.     }
  531.     j++ ;
  532.  
  533.       } /* end while */
  534.     } /* end for i */
  535. }
  536.